import dayjs from 'dayjs'

// 格式化时间
export const formatTime = (time) => {
    let year = dayjs(time).year();
    let month = dayjs(time).month() + 1;
    let date = dayjs(time).date();
    let hour = dayjs(time).hour();
    let minute = dayjs(time).minute();

    if (month < 10) {
        month = '0' + month;
    }
    if (date < 10) {
        date = '0' + date;
    }
    if (hour < 10) {
        hour = '0' + hour;
    }
    if (minute < 10) {
        minute = '0' + minute;
    }
    return year + '-' + month + '-' + date + ' ' + hour + ':' + minute;
}

// 获取当前日期和一周前日期 格式为YYYY-MM-DD
export const formatDate = (date) => {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2)
        month = '0' + month;
    if (day.length < 2)
        day = '0' + day;

    return [year, month, day].join('-');
}  

// 获取一个日期的前N天或者后N天
export const getDay = (date, day) => {
    var dd = new Date(date);
    dd.setDate(dd.getDate() + day);
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1;
    var d = dd.getDate();
    if (m < 10) {
        m = '0' + m;
    }
    if (d < 10) {
        d = '0' + d;
    }
    return y + "-" + m + "-" + d;
}
